{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Getting the mid-circuit states\n", "\n", "Usually, we only see the final results of the execution of our circuit. However, occasionally it is helpful to know how the state of the system evolves during the execution of the circuit, particularly if we are interested in the statevector and the density matrix of the system.\n", "\n", " It is necessary to state that on real quantum computers, it is not necessarily possible to get the mid-circuit states (and the measurements also), so this is an excellent functionality for the emulator.\n", "\n", "To achieve this, we will use barriers supported by Qiskit and OpenQASM standards. We will get the results of the states of the system at the location where the barrier is placed. Also, it is crucial to notice that the barriers divide the circuit for transpilation. Hence, they will behave as a separator for the states and the transpilation process.\n", "\n", "First, we will demonstrate the usage of the barriers. Afterward, we will retrieve the desired states at the given time.\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-10-08T14:51:47.024962Z", "start_time": "2024-10-08T14:51:46.726297Z" } }, "outputs": [], "source": [ "# For more information see notebooks 2 & 3\n", "from c12_callisto_clients.user_configs import UserConfigs\n", "from c12_callisto_clients.qiskit.c12sim_provider import C12SimProvider\n", "import os\n", "user_auth_token = os.getenv(\"C12_TOKEN\")\n", "configs = UserConfigs.parse_obj({\"token\" : user_auth_token})\n", "c12_simulator_provider = C12SimProvider(configs)\n", "c12_simulator_backend = c12_simulator_provider.get_backend('c12sim-iswap')" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-10-08T14:51:51.979025Z", "start_time": "2024-10-08T14:51:51.884106Z" } }, "outputs": [ { "data": { "text/plain": " ┌───┐ ░ ░ \nq_0: ┤ H ├─░───■───░──────\n └───┘ ░ ┌─┴─┐ ░ ┌───┐\nq_1: ──────░─┤ X ├─░─┤ X ├\n ░ └───┘ ░ └───┘", "text/html": "
┌───┐ ░ ░ \nq_0: ┤ H ├─░───■───░──────\n └───┘ ░ ┌─┴─┐ ░ ┌───┐\nq_1: ──────░─┤ X ├─░─┤ X ├\n ░ └───┘ ░ └───┘" }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from qiskit import QuantumCircuit\n", "\n", "# For more info about Qiskit see notebook 1 and Qiskit's documentation\n", "circuit = QuantumCircuit(2)\n", "circuit.h(0)\n", "circuit.barrier() # We add a barrier at this position\n", "circuit.cx(0, 1)\n", "circuit.barrier()\n", "circuit.x(1)\n", "\n", "circuit.draw()\n", "# Notice the barrier position in the circuit" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-10-08T14:52:48.880150Z", "start_time": "2024-10-08T14:51:56.241718Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "C12 simulation counts: {'00': 13, '01': 4999, '10': 4981, '11': 7}\n" ] } ], "source": [ "# Running the circuit on Callisto\n", "c12_job = c12_simulator_backend.run(circuit, shots=10000)\n", "c12_result = c12_job.result()\n", "c12_counts = c12_result.get_counts()\n", "print(f\"C12 simulation counts: {c12_counts}\")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false, "ExecuteTime": { "end_time": "2024-10-08T14:53:23.952461Z", "start_time": "2024-10-08T14:53:23.446790Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "14a1833f-f695-42ba-b665-58dae6f9f5eb\n" ] }, { "data": { "text/plain": "